package com.example.reference_book.service.impl;

import com.example.reference_book.entity.Book;
import com.example.reference_book.entity.BookTag;
import com.example.reference_book.dao.BookTagDao;
import com.example.reference_book.service.BookTagService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 书籍标签表(BookTag)表服务实现类
 *
 * @author makejava
 * @since 2022-06-12 17:04:46
 */
@Service("bookTagService")
public class BookTagServiceImpl implements BookTagService {
    @Resource
    private BookTagDao bookTagDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BookTag queryById(Integer id) {
        return this.bookTagDao.queryById(id);
    }

    @Override
    public List<BookTag> queryByTagId(Integer tagid) {
        return this.bookTagDao.queryByTagId(tagid);
    }



    /**
     * 分页查询
     *
     * @param bookTag 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<BookTag> queryByPage(BookTag bookTag, PageRequest pageRequest) {
        long total = this.bookTagDao.count(bookTag);
        return new PageImpl<>(this.bookTagDao.queryAllByLimit(bookTag, pageRequest), pageRequest, total);
    }

    @Override
    public List<Book> bookandtag(Integer tagid) {
        return this.bookTagDao.bookandtag(tagid);
    }

    /**
     * 新增数据
     *
     * @param bookTag 实例对象
     * @return 实例对象
     */
    @Override
    public BookTag insert(BookTag bookTag) {
        this.bookTagDao.insert(bookTag);
        return bookTag;
    }

    /**
     * 修改数据
     *
     * @param bookTag 实例对象
     * @return 实例对象
     */
    @Override
    public BookTag update(BookTag bookTag) {
        this.bookTagDao.update(bookTag);
        return this.queryById(bookTag.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.bookTagDao.deleteById(id) > 0;
    }
}
